package cn.enjoy.config;


import com.bstek.ureport.console.UReportServlet;
import com.bstek.ureport.definition.datasource.BuildinDatasource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

import javax.annotation.Resource;
import javax.servlet.Servlet;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * 不加项目能够启动但是会导致加载数据源报错或加载不了
 *
 * @author K
 * @date 2021/3/2 17:55
 */
@ImportResource("classpath:ureport-console-context.xml")
@Configuration
@EnableAutoConfiguration
public class ReportConfig implements BuildinDatasource {

    @Resource
    DataSource dataSource;
    private Logger log = LoggerFactory.getLogger(getClass());

    /**
     * 定义ureport的启动servlet /ureport/* 不可更改
     * 访问 http://localhost:9000/ureport/designer
     *
     * @return ServletRegistrationBean<Servlet>
     */
    @Bean
    public ServletRegistrationBean<Servlet> registrationBean() {
        return new ServletRegistrationBean<>(new UReportServlet(), "/ureport/*");
    }

    @Override
    public String name() {
        return "ureport2 数据源";
    }

    @Override
    public Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            log.error("Ureport 数据源 获取连接失败！");
            e.printStackTrace();
        }
        return null;
    }

}
